package ${packagePrefix}.${domainObjectName?lower_case}.controller;

import ${packagePrefix}.${domainObjectName?lower_case}.dto.${domainObjectName}ExcelDTO;
import ${packagePrefix}.${domainObjectName?lower_case}.service.${domainObjectName}ExcelService;
import ${packagePrefix}.common.api.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

<#if generateComments>
/**
 * ${domainObjectName} Excel控制器
 * <p>
 * 提供Excel导入导出相关的Web接口
 * </p>
 *
 * @author Auto Generated
 */
</#if>
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/${domainObjectName?lower_case}/excel")
@Tag(name = "${domainObjectName}Excel管理", description = "${domainObjectName}数据的Excel导入导出")
public class ${domainObjectName}ExcelController {

    private final ${domainObjectName}ExcelService excelService;
    
    <#if generateComments>/**
     * 获取Excel导入模板
     *
     * @param response HTTP响应对象
     */
    </#if>
    @GetMapping("/template")
    @Operation(summary = "获取${domainObjectName}Excel导入模板", description = "下载${domainObjectName}数据导入的Excel模板")
    public void getExcelTemplate(HttpServletResponse response) {
        excelService.getExcelTemplate(response, "${domainObjectName}导入模板");
    }
    
    <#if generateComments>/**
     * 导入Excel数据
     *
     * @param file 上传的Excel文件
     * @return 导入结果
     */
    </#if>
    @PostMapping("/import")
    @Operation(summary = "导入${domainObjectName}Excel数据", description = "通过Excel批量导入${domainObjectName}数据")
    public Response<Integer> importExcel(
            @Parameter(description = "Excel文件") @RequestParam("file") MultipartFile file) {
        // 使用AtomicInteger来跟踪导入进度
        AtomicInteger successCount = new AtomicInteger(0);
        
        // 调用服务层处理Excel数据
        int importCount = excelService.importExcel(file, dataList -> {
            // 这里处理批次数据，例如保存到数据库等操作
            // 示例：增加成功计数
            successCount.addAndGet(dataList.size());
            
            // TODO: 在实际实现中，调用业务服务处理数据
            log.info("处理Excel导入数据，批次数量: {}", dataList.size());
        });
        
        return Response.success(importCount, "成功导入" + successCount.get() + "条数据");
    }
    
    <#if generateComments>/**
     * 导出Excel数据
     *
     * @param response HTTP响应对象
     */
    </#if>
    @GetMapping("/export")
    @Operation(summary = "导出${domainObjectName}Excel数据", description = "将${domainObjectName}数据导出为Excel文件")
    public void exportExcel(HttpServletResponse response) {
        // TODO: 在实际实现中，从业务服务获取要导出的数据
        List<${domainObjectName}ExcelDTO> dataList = List.of();
        
        excelService.exportExcel(response, dataList, "${domainObjectName}数据");
    }
} 